


function set_cookie(name, value, exdays){

    let _exdays = new Date()
    _exdays.setTime(_exdays.getTime() + exdays * 24 * 60 * 60 * 1000)
    document.cookie = `${name}=${value};expires=${_exdays.toGMTString()}`
}

function get_cookie(name){
    let reg = new RegExp(`${name}\\s*\\=\\s*(.+?)\\s*`)
    for(let elem of document.cookie.split(';')){
        let m = elem.match(reg)
        if(m) return m[1]
    }
}

function guid(){
    return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, c => {
        let r = Math.random() * 16 | 0
        let v = 'x' === c ? r : (r & 0x3 | 0x8)
        return v.toString(16)
    })
}

function contain_no_seq(father, child){
    let map = {}
    father.forEach(elem=>{map[elem] = 1})
    for(let elem of child){
        if(undefined === map[elem]) return false
    }
    return true
}

function contain_seq(father, child){
    let map = {}
    father.forEach((elem, i) => {
        map[elem] = map[elem] || []
        map[elem].push(i)
    })
    let pr = -1
    for(let elem of child){
        if(undefined === map[elem]) return false
        let cur = map[elem].shift()
        if(undefined === cur || pr > cur) return false
        pr = cur
    }
    return true
}

function copy(obj){
    let res = {}
    for(let k in obj){
        res[k] = obj[k]
    }
    return res
}

function deep_copy(obj){
    if(obj instanceof Array){
        let arr = []
        obj.forEach(elem=>{
            arr.push(deep_copy(elem))
        })
        return arr
    }else if('object' === typeof obj){
        let obj2 = {}
        for(let k in obj){
            obj2[k] = deep_copy(obj[k])
        }
        return obj2
    }else{
        return obj
    }
}

function camel2underline(str){
    return str.replace(/[A-Z]/g, s=>{
        return ' ' + s.toLowerCase()
    }).replace(/(\.\s+)|(^\s+)|(\s+$)/g, key=>{
        return key.indexOf('.') > -1 ? '.' : ''
    }).replace(/\s+/g, '_')
}

module.exports = {
    guid, contain_seq, contain_no_seq, copy, deep_copy,
    camel2underline, set_cookie, get_cookie,
}